package com.example.springboot.service.impl;

import com.example.springboot.controller.request.BaseRequest;
import com.example.springboot.entity.Exhibition;
import com.example.springboot.exception.ServiceException;
import com.example.springboot.mapper.ExhibitionMapper;
import com.example.springboot.service.IExhibitionService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Slf4j
@Service
public class ExhibitionService implements IExhibitionService {

    @Autowired //通过autowired导入exhibitionMapper
    ExhibitionMapper exhibitionMapper;

    public List<Exhibition> list(){
        return exhibitionMapper.list();
    }

    @Override
    public PageInfo<Exhibition> page(BaseRequest baseRequest){
//        dependency引入pagehelper
//        PageHelper传入页面和每页信息条数
        PageHelper.startPage(baseRequest.getPageNum(),baseRequest.getPageSize());
        List<Exhibition> exhibitions=exhibitionMapper.listByStatus(baseRequest);
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
        LocalDate now = LocalDate.now();

        for(Exhibition exhibition: exhibitions){

            LocalDate returnDate = LocalDate.parse(exhibition.getEndTime());//时间格式修改
            LocalDate startTime = LocalDate.parse(exhibition.getStartTime());
            if(now.isEqual(returnDate)){
//                当前日期比归还的日期小一天
                exhibition.setStatus("即将闭展");
            }else if(now.isAfter(returnDate)){
                exhibition.setStatus("已结束");
            }else if(startTime.isAfter(now)){
                exhibition.setStatus("未开展");
            }else{
                exhibition.setStatus("在展");
            }
            exhibitionMapper.updateById(exhibition);
        }
        System.out.println(exhibitions);
        return new PageInfo<>(exhibitions);
    }

    @Override
    public PageInfo<Exhibition> userPage(BaseRequest baseRequest){
//        dependency引入pagehelper
//        PageHelper传入页面和每页信息条数
        PageHelper.startPage(baseRequest.getPageNum(),baseRequest.getPageSize());
        List<Exhibition> exhibitions=exhibitionMapper.listByStatus(baseRequest);
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
        LocalDate now = LocalDate.now();

        for(Exhibition exhibition: exhibitions){
            LocalDate returnDate = LocalDate.parse(exhibition.getEndTime());//时间格式修改
            LocalDate startTime = LocalDate.parse(exhibition.getStartTime());
            if(now.isEqual(returnDate)){
//                当前日期比归还的日期小一天
                exhibition.setStatus("即将结束");
            }else if(now.isAfter(returnDate)){
                exhibition.setStatus("已结束");
            }else if(startTime.isAfter(now)){
                exhibition.setStatus("未开展");
            }else{
                exhibition.setStatus("在展");
            }
            exhibitionMapper.updateById(exhibition);
        }
        return new PageInfo<>(exhibitions);
    }

    @Override
    public void save(Exhibition exhibition){//        当做卡号来处理
        Date date= new Date();
//     通过hashCode编码将fastSimpleUUID产生的字母和数字转为数字
        try{
            exhibitionMapper.save(exhibition);
        }catch(DuplicateKeyException e){
            log.error("数据插入失败,展会name:{}",exhibition.getName());
            throw new ServiceException("用户插入失败");
        }
    }


    @Override
    public Exhibition getById(Integer id) {
        return exhibitionMapper.getById(id);
    }

    @Override
    public void update(Exhibition exhibition) {
        exhibition.setUpdateTime(new Date());
        exhibitionMapper.updateById(exhibition);

    }

    @Override
    public void deleteById(Integer id) {
        exhibitionMapper.deleteById(id);
    }
    
}
